package com.hy.mvc.biz.excel.exportstrategy;

import com.hy.mvc.biz.excel.model.SimpleExcelModel;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.springframework.stereotype.Component;

/**
 * Created by Booker on 2017/7/3.
 */
@Component("simpleHeadStrategy")
public class SimpleHeadStrategy implements HeadStrategy {
    @Override
    public HSSFSheet buildHeaders(HSSFSheet workSheet, SimpleExcelModel excelModel) {
        // Header字体
        Font font = workSheet.getWorkbook().createFont();
        font.setBoldweight(Font.BOLDWEIGHT_BOLD);

        // 单元格样式
        HSSFCellStyle headerCellStyle = workSheet.getWorkbook().createCellStyle();
        headerCellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
        headerCellStyle.setFillForegroundColor(HSSFColor.PALE_BLUE.index);
        headerCellStyle.setAlignment(CellStyle.ALIGN_CENTER);
        headerCellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
        headerCellStyle.setWrapText(true);
        headerCellStyle.setFont(font);
        headerCellStyle.setBorderBottom(CellStyle.BORDER_THIN);

        // 创建字段标题
        int startRowIndex = workSheet.getLastRowNum() + 1;
        HSSFRow rowHeader = workSheet.createRow((short) startRowIndex);
        rowHeader.setHeight((short) 500);

        String[] heads = excelModel.getHeader();
        int startColIndex = 0;
        for (int i = 0; i < heads.length; i++) {
            HSSFCell cell = rowHeader.createCell(startColIndex + i);
            cell.setCellValue(heads[i]);
            cell.setCellStyle(headerCellStyle);
        }

        return workSheet;
    }

}
